package model

import (
	"fmt"
	"choujiang/common/db"
)

type Game struct {
	Id      int64  `xorm:"INT(11) not null pk autoincr"`
	GameId  string `xorm:"VARCHAR(255) not null"`
	Name    string `xorm:"VARCHAR(60) not null unique"`
	WxAppId string `xorm:"VARCHAR(60) not null unique"`

	AppType       string   `xorm:"VARCHAR(60) not null default 0 comment('0未知/1男/2女')"`
	Path          string   `xorm:"VARCHAR(255) not null"`
	Platform      int64    `xorm:"INT(11) not null"`
	Status        int64    `xorm:"INT(11) not null default 0"`
	Category      int64    `xorm:"INT(11) not null default 0"`
	Icon          string   `xorm:"VARCHAR(255) not null default ''"`
	Cover         string   `xorm:"VARCHAR(255) not null default ''"`
	Order         int64    `xorm:"INT(11) not null default 0"`
	Desc          string   `xorm:"VARCHAR(100) not null default ''"`
	IsRecommend   int64    `xorm:"INT(11) not null default 0"`
	Reward        int64    `xorm:"INT(11) not null default 0"`
	Price         float64  `xorm:"DECIMAL(10) default 0.00"`
	Balance       float64  `xorm:"DECIMAL(10) default 0.00"`
	Special       int64    `xorm:"INT(11) not null default 0"`
	IsSdk         int64    `xorm:"INT(11) not null default 0"`
	AccumPromote  int64    `xorm:"INT(11) not null default 0"`
	QrCode        string   `xorm:"VARCHAR(100)"`
	CateName      []string `xorm:"-"`
	TagName       []string `xorm:"-"`
	Earning       float64  `xorm:"-"`
	PromoteUser   []string `xorm:"-"`
	PromoteNumber int      `xorm:"-"`
	InviterEarn   float64  `xorm:"-"`
	PlayerEarn    float64  `xorm:"-"`
	Played        bool     `xorm:"-"`

	CreatedAt int64 `xorm:"not null default 0 INT(11) created"`
	UpdatedAt int64 `xorm:"not null default 0 INT(11) updated"`
}

func (o *Game) Insert() (*Game, error) {
	_, err := db.MysqlEngine.InsertOne(o)
	return o, err
}

func (o *Game) Update() (*Game, error) {
	_, err := db.MysqlEngine.ID(o.Id).Update(o)
	return o, err
}

func GetGameByGameId(gameId string) *Game {
	game := new(Game)
	has, err := db.MysqlEngine.Where("game_id=?", gameId).Get(game)
	fmt.Printf("GetUserByMiniUnionId err %+v\n", err)
	if !has || err != nil {
		return nil
	}
	return game
}

func GetGameById(id int64) *Game {
	game := new(Game)
	has, err := db.MysqlEngine.Where("id=?", id).Get(game)
	fmt.Printf("GetUserByMiniUnionId err %+v\n", err)
	if !has || err != nil {
		return nil
	}
	return game
}
